package com.authine.cloudpivot.ext.workflowevent.listener;

import com.authine.hermes.app.flow.engine.delegate.event.EventType;
import com.authine.hermes.app.flow.engine.delegate.event.WorkflowEvent;
import com.authine.hermes.app.flow.engine.delegate.event.impl.FlowActivityEventImpl;
import com.authine.hermes.app.flow.engine.delegate.event.impl.WorkItemEventImpl;
import com.authine.hermes.app.flow.engine.impl.persistence.entity.WorkItemEntity;
import com.authine.hermes.app.flow.runtime.engine.listener.AbstractWorkflowEventListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyWorkflowEventListener extends AbstractWorkflowEventListener {
    @Override
    public void onEvent(WorkflowEvent workflowEvent) {
        log.info("----------------------------------------------------");
        log.info("流程code:{},流程id:{},variables:{}", workflowEvent.getWorkflowCode(), workflowEvent.getWorkflowInstanceId(), workflowEvent.getVariables());
        log.info("----------------------------------------------------");
        EventType eventType = workflowEvent.getType();
        if (EventType.WF_START == eventType) {
            log.info("####################################################");
            log.info("####### 1. 流程开始了");
        }

        if (EventType.WF_END == eventType) {
            log.info("####################################################");
            log.info("####### 2. 流程结束了");
        }

        if (EventType.WF_ABORT == eventType) {
            log.info("####################################################");
            log.info("####### 3. 流程作废了");
        }

        if (EventType.WF_ACTIVATE == eventType) {
            log.info("####################################################");
            log.info("####### 4. 流程激活后");
        }

        if (EventType.ACTIVITY_ACTIVATED == eventType) {
            FlowActivityEventImpl activityEvent = (FlowActivityEventImpl) workflowEvent;
            log.info("####################################################");
            log.info("####### 5. 活动激活后,活动code：{}", activityEvent.getActivityCode());
        }

        if (EventType.ACTIVITY_ACTIVATING == eventType) {
            FlowActivityEventImpl activityEvent = (FlowActivityEventImpl) workflowEvent;
            log.info("####################################################");
            log.info("####### 6. 活动激活前,活动code：{}", activityEvent.getActivityCode());
        }

        if (EventType.ACTIVITY_FINISHED == eventType) {
            FlowActivityEventImpl activityEvent = (FlowActivityEventImpl) workflowEvent;
            log.info("####################################################");
            String workflowCode = activityEvent.getWorkflowCode();
        }

        if (EventType.ACTIVITY_CANCELED == eventType) {
            FlowActivityEventImpl activityEvent = (FlowActivityEventImpl) workflowEvent;
            log.info(" 12. 活动取消了,活动code：{}", activityEvent.getActivityCode());
        }

        if (EventType.ENTITY_CREATED == eventType) {
            log.info(" 13. 创建了1个待办任务");
        }

        if (EventType.ENTITY_UPDATED == eventType) {
            log.info(" 14. 更新了1个待办任务");
        }

        if (EventType.ENTITY_DELETED == eventType) {
            log.info(" 15. 删除了1个待办任务");
        }

        if (EventType.WORKITEM_SUBMITTED == eventType) {
            //未打印日志
            WorkItemEventImpl workItemEvent = (WorkItemEventImpl) workflowEvent;
            log.info("*************************************************");
            WorkItemEntity workItemEntity = workItemEvent.getWorkItemEntity();
            log.info(" 16. 任务提交后,活动code：{},处理人:{},BOId:{}", workItemEntity.getActivityCode(),
                    workItemEntity.getParticipant() + "-" + workItemEntity.getParticipantName(),
                    this.getFlowInstanceEntityById(workItemEvent.getWorkflowInstanceId()).getBizObjectId());
            log.info("*************************************************");
        }

        if (EventType.WORKITEM_REJECTED == eventType) {
            WorkItemEventImpl workItemEvent = (WorkItemEventImpl) workflowEvent;
            log.info(" 17. 任务驳回后,活动code：{}", workItemEvent.getActivityCode());
        }
    }
}